// pages/repairman-intro/repairman-intro.js
const app = getApp();
var util = require('../../utils/util.js');
var oderId, index, page, limit = 5, repairId, preventResubmit;
var _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    repairmanIntro: '',
    evaluateRepairMan: [],
    buttonClicked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this;    
    if (options.oderId){
      oderId = options.oderId;
      index = options.index;
      preventResubmit = options.preventResubmit;
      repairId = app.globalData.repairmanList[index].code;
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    _this.setData({
      repairmanIntro: app.globalData.repairmanList[index],
      preventResubmit: preventResubmit
    })
    //_this.repairManMassage(repairId);
    page = 1;
    _this.evaluateRepairManList(page, limit);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // 维修师评价
  evaluateRepairManList: function (a, b){
    app.evaluateRepairMan(repairId, a, b, function (res) {
      console.log(res);
      _this.setData({
        evaluateRepairMan: res.results
      })
      page++;
    })
  },
  // 当页评论大于5条才能刷新
  rmScrolltolower:function(){
    var evaluateRepairMan = _this.data.evaluateRepairMan;
    if (evaluateRepairMan.length == 5){
      evaluateRepairManList(repairId, page, limit);
    }
  },
  // 选择维修师
  chooseRepairman: function (e) {
    var payload = {};
    payload.id = oderId;
    payload.repairmanCode = _this.data.repairmanIntro.code;
    if (_this.data.preventResubmit == "true") {
      // 返回上一个页面
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];  //上一个页面
      prevPage.setData({
        preventResubmit: false,
      })
      _this.setData({
        preventResubmit: false
      })
      app.mtChooseMaintenance(payload, function (res) {
        if (res) {
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 1500,
            mask: true,
            success: function () {
              if (!_this.data.buttonClicked) {
                util.buttonClicked(_this);
                wx.navigateTo({
                  url: '/pages/service-detail/service-detail?id=' + oderId + '&type=ing&tab=1',
                })
              }
            }
          })
        } else {
          util.alertMsg('提示', '选择维修师失败，请稍后再试。', function () {
            _this.backIndex();
            return;
          }, function () {
            _this.backIndex();
            return;
          });
        }
      })
    } else {
      util.alertMsg('提示', '不能重复提交', function () {
        _this.backIndex();
        return;
      }, function () {
        _this.backIndex();
        return;
      });
    }
  },
  // 打电话
  makePhoneCall: function (e) {
    var phoneNumber = e.currentTarget.dataset.phoneNumber;
    wx.makePhoneCall({
      phoneNumber: phoneNumber,
    })
  },
  // 关闭所有页面打开首页
  backIndex: function () {
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.reLaunch({
        url: '/pages/index/index',
      })
    }
  }
})